package cn.itcast.spring.demo2;


import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;


public class OrderServiceProxy implements MethodInterceptor{

	
	private OrderServiceImpl orderServiceImpl;
	
	public OrderServiceProxy(OrderServiceImpl orderServiceImpl){
		this.orderServiceImpl = orderServiceImpl;
	}
	
	
	
	public OrderServiceImpl createOrderServiceImpl(){
		Enhancer enhancer = new Enhancer();
		//设置父类
		enhancer.setSuperclass(orderServiceImpl.getClass());
		//设置回调
		enhancer.setCallback(this);
		
		OrderServiceImpl create = (OrderServiceImpl) enhancer.create();
		return create;
		
	}



	@Override
	public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
		if(method.getName().equals("add")){
			System.out.println("权限的校验");
			Object invokeSuper = methodProxy.invokeSuper(object, args);
			return invokeSuper;
		}else if(method.getName().equals("delete")){
			Object invokeSuper = 	methodProxy.invokeSuper(object, args);
			System.out.println("日志的记录");
			return invokeSuper;
		}
		return methodProxy.invokeSuper(object, args);
	}



	
	
	

}
